package net.zhengxinyun.unifyApp.ctrl;

import com.sirdc.modules.utils.StringUtils;

import com.yunpian.sdk.model.SmsSingleSend;
import net.zhengxinyun.unifyApp.core.Result;
import net.zhengxinyun.unifyApp.core.ResultUtils;
import net.zhengxinyun.unifyApp.filter.LoginFilter;
import net.zhengxinyun.unifyApp.filter.UserFilter;
import net.zhengxinyun.unifyApp.service.user.UserSerivce;
import net.zhengxinyun.unifyApp.yunpian.YunpianService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/app")
public class LoginCtrl {


    @Autowired
    private UserSerivce userSerivce;

    @Autowired
    private YunpianService yunpianService;
    /**
     * 登录
     *
     */
    @PostMapping("/login")
    public Result<HashMap<String,Object>> login(@RequestBody UserFilter filter)
    {
       HashMap<String, Object> map = userSerivce.vaildCode(filter.getUserTel(), filter.getVaildCode());//校验验证码；
        int  code =(int)map.get("code");
        if(200!=code)
        {
            Object mes = map.get("msg");
            return ResultUtils.getResults(Result.Status.ERROR,mes+"",null);
        }

       // HashMap<String,Object> map = new HashMap<>();

       /* if(!"6666".equals(filter.getVaildCode()))
        {
            return ResultUtils.getResults(Result.Status.ERROR,"验证码有误",null);
        }*/


        List<UserFilter> userFilters = userSerivce.selectSysbyId(filter.getUserTel());

        for (UserFilter userFilter : userFilters) {
            if("JX".equals(userFilter.getSysCode()))
            {
                String sysRights = userFilter.getSysRights();
                if ("乡镇".equals( userFilter.getRole()))
                {
                    userFilter.setSysRights("9,10");
                }else if("县直单位".equals(userFilter.getRole()))
                {
                    userFilter.setSysRights("11,12,2,3,4,7,8");
                }else if("县领导".equals(userFilter.getRole()))
                {
                    userFilter.setSysRights(null);
                }else {
                    userFilter.setSysRights(sysRights);
                }
            }
            if ("DC".equals(userFilter.getSysCode())){
                //督查系统
                /*APP按钮功能排序
                1、收藏、
                2、评价
                3、评价任务
                4、已完成
                5、全部任务
                6、用户管理
                7、立项
                8、呈阅件
                9、县直单位排名
                10、领导批示
                11、县领导排名
                12、领导排名、*/
                String sysRights = userFilter.getSysRights();
                if ("督查组".equals(userFilter.getDeptType())){
                    if ("主要领导".equals(userFilter.getRole())){
                        userFilter.setSysRights("1,2,3,4,5,6,7,8");
                    }else if ("分管领导".equals(userFilter.getRole())||"经办人".equals(userFilter.getRole())){
                        userFilter.setSysRights("1,2,3,4,5,7,8");
                    }
                }else if ("承办单位".equals(userFilter.getDeptType())){
                    if ("主要领导".equals(userFilter.getRole())){
                        userFilter.setSysRights("1,2,3,4,5,6,8");
                    }else if ("分管领导".equals(userFilter.getRole())||"经办人".equals(userFilter.getRole())){
                        userFilter.setSysRights("1,2,3,4,5,8");
                    }
                }
                if ("县领导".equals(userFilter.getRole())){
                    userFilter.setSysRights("");
                }
                if ("县主官".equals(userFilter.getRole())){
                    userFilter.setSysRights("");
                }
            }
        }

        //去重
      /*  userFilters = userFilters.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(UserFilter::getSysCode))), ArrayList::new));*/

        map.put("userInfo",userFilters);


        return ResultUtils.getSuccessResults(map);
    }


    /**
     * 发送验证码
     */
    @PostMapping("/sendValidCode")
    public Result<String> sendVaildCode(@RequestBody LoginFilter filter){
        if(StringUtils.isBlank(filter.getUserTel()))
        {
            return ResultUtils.getResults(Result.Status.ERROR,"参数有误");

        }

       //判断用户存在不存在；
        String user = userSerivce.vaildUser(filter.getUserTel());

        if(StringUtils.isBlank(user))
        {
            return  ResultUtils.getResults(Result.Status.ERROR,"对不起，找不到该用户");
        }
        //String vaildCode = "6666";
        //  验证码发送 --start---
        String vaildCode = Integer.toString((int)((Math.random()*9+1)*1000));
        String mes ="您的验证码是"+vaildCode+"。如非本人操作，请忽略本短信";
        com.yunpian.sdk.model.Result<SmsSingleSend> smsSingleSendResult = yunpianService.sendSmS(filter.getUserTel(), mes);
         if (smsSingleSendResult.getCode()!=0)
        {
            return  ResultUtils.getResults(Result.Status.ERROR,"验证码获取失败");
        }
        // 验证码发送---end----
        userSerivce.saveVaildCode(filter.getUserTel(),vaildCode);
        return  ResultUtils.getResults(Result.Status.SUCCESS,"验证码发送成功");
    }

}
